package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.product.api.domain.vo.BrandVo;
import com.spzx.product.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: BrandServiceImpl
 * Package: com.spzx.product.service.impl
 * Description:
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BaseMapper<Brand>,Brand> implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> selectBrandList(Brand brand) {
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(brand.getName()),Brand::getName,brand.getName());
        queryWrapper.like(StringUtils.hasText(brand.getRemark()),Brand::getRemark,brand.getRemark());
        queryWrapper.eq(StringUtils.hasText(brand.getLogo()),Brand::getLogo,brand.getLogo());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public int insertBrand(Brand brand) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brand.getName()));
        if (count > 0) {
            throw new ServiceException("该品牌已存在");
        }
        return baseMapper.insert(brand);
    }

    @Override
    public int updateBrand(Brand brand) {
        Brand brandDB = baseMapper.selectOne(new LambdaQueryWrapper<Brand>().eq(Brand::getId, brand.getId()));
        if (brandDB!=null) {
            if (brandDB.getName().equals(brand.getName())) {
                return 1;
            }
        }
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brand.getName()));
        if (count > 0) {
            throw new ServiceException("该品牌已存在");
        }

        return baseMapper.updateById(brand);
    }

    @Override
    public List<BrandVo> getAllBrandList() {
        return brandMapper.getAllBrand();
    }

}
